package httpCtrler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	st "kjjcgroup_server/model"
	sv "kjjcgroup_server/service"
	"net/http"
	"strconv"
	"time"
)

func ProfessionRouter(router *gin.Engine, routerAuth *gin.RouterGroup) {
	router.GET("/profession", profession)
	LogMap["/profession"] = "分页查询专业"
	router.GET("/professionList", professionList)
	LogMap["/professionList"] = "查询专业"
	routerAuth.PUT("/professionAdd", professionAdd)
	LogMap["/professionAdd"] = "添加专业"
	routerAuth.PUT("/professionEdit", professionEdit)
	LogMap["/professionEdit"] = "编辑专业"
	routerAuth.DELETE("/professionDel", professionDel)
	LogMap["/professionDel"] = "删除专业"
	router.GET("/professionType", professionType)
	LogMap["/professionType"] = "查询专业分类"
	routerAuth.PUT("/professionTypeAdd", professionTypeAdd)
	LogMap["/professionTypeAdd"] = "添加专业分类"
	routerAuth.PUT("/professionTypeEdit", professionTypeEdit)
	LogMap["/professionTypeEdit"] = "编辑专业分类"
	routerAuth.DELETE("/professionTypeDel", professionTypeDel)
	LogMap["/professionTypeDel"] = "删除专业分类"
}

func profession(c *gin.Context) {
	ProfessionId := c.Query("ProfessionId")
	Recommend := c.Query("Recommend")
	ProfessionTitle := c.Query("profession")
	TypeId := c.Query("TypeId")
	Keyword := c.Query("Keyword")

	pageIdx, pageCnt := Page(c)
	if pageIdx == -1 {
		pageIdx = 0
	}
	if pageCnt == -1 {
		pageCnt = 10
	}
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)
	var collArr []*st.ProfessionMod
	var collArr1 []*st.ProfessionMod
	var count int64
	if ProfessionId != "" {
		err = mysql.Where("profession_id=?", ProfessionId).Find(&collArr)
		_, _ = mysql.Where("profession_id=?", ProfessionId).Update(&st.ProfessionMod{ClicksNumber: collArr[0].ClicksNumber + 1})
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else if Keyword != "" {
		err = mysql.Where("profession_title LIKE ? or profession_sub_title  LIKE ?  or profession_des  LIKE ? ", "%"+Keyword+"%", "%"+Keyword+"%", "%"+Keyword+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("profession_title LIKE ? or profession_sub_title  LIKE ?  or profession_des  LIKE ? ", "%"+Keyword+"%", "%"+Keyword+"%", "%"+Keyword+"%").FindAndCount(&collArr1)
	} else if Recommend != "" {
		err = mysql.Where("recommend=?", Recommend).Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("recommend=?", Recommend).FindAndCount(&collArr1)
	} else if ProfessionTitle != "" {
		err = mysql.Where("profession_title LIKE ?", "%"+ProfessionTitle+"%").Or("profession_des LIKE ?", "%"+ProfessionTitle+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("profession_title LIKE ?", "%"+ProfessionTitle+"%").FindAndCount(&collArr1)
	} else if TypeId != "" {
		err = mysql.Where("type_id LIKE ?", "%"+TypeId+"%").Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.Where("type_id LIKE ?", "%"+TypeId+"%").FindAndCount(&collArr1)
	} else {
		err = mysql.Limit(pageCnt, pageIdx*pageCnt).OrderBy("create_time desc").Find(&collArr)
		count, _ = mysql.FindAndCount(&collArr1)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	rtnDt := rtnData(count, pageCnt, pageIdx, collArr)
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: rtnDt})
}
func professionList(c *gin.Context) {
	ProfessionId := c.Query("ProfessionId")
	Recommend := c.Query("Recommend")
	ProfessionTitle := c.Query("profession")
	TypeId := c.Query("TypeId")

	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)
	var collArr []*st.ProfessionMod

	if ProfessionId != "" {
		err = mysql.Where("profession_id=?", ProfessionId).Find(&collArr)
		_, _ = mysql.Where("profession_id=?", ProfessionId).Update(&st.ProfessionMod{ClicksNumber: collArr[0].ClicksNumber + 1})
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else if Recommend != "" {
		err = mysql.Where("recommend=?", Recommend).OrderBy("create_time desc").Find(&collArr)
	} else if ProfessionTitle != "" {
		err = mysql.Where("profession_title LIKE ?", "%"+ProfessionTitle+"%").Or("profession_des LIKE ?", "%"+ProfessionTitle+"%").OrderBy("create_time desc").Find(&collArr)
	} else if TypeId != "" {
		err = mysql.Where("type_id LIKE ? and recommend=?", "%"+TypeId+"%", 1).OrderBy("create_time desc").Find(&collArr)
	} else {
		err = mysql.OrderBy("create_time desc").Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func professionAdd(c *gin.Context) {
	resDt := &st.ProfessionMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Cols("type_id", "profession_title", "profession_sub_title", "profession_des", "profession_img", "profession_con", "recommend", "user_account", "clicks_number", "create_time").InsertOne(&st.ProfessionMod{TypeId: resDt.TypeId, ProfessionTitle: resDt.ProfessionTitle, ProfessionSubTitle: resDt.ProfessionSubTitle, ProfessionDes: resDt.ProfessionDes, ProfessionImg: resDt.ProfessionImg, ProfessionCon: resDt.ProfessionCon, Recommend: resDt.Recommend, UserAccount: resDt.UserAccount, ClicksNumber: resDt.ClicksNumber, CreateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: resDt.ProfessionTitle + "添加成功!"})
}

func professionEdit(c *gin.Context) {
	resDt := &st.ProfessionMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Where("profession_id=?", resDt.ProfessionId).Update(&st.ProfessionMod{TypeId: resDt.TypeId, ProfessionTitle: resDt.ProfessionTitle, ProfessionSubTitle: resDt.ProfessionSubTitle, ProfessionDes: resDt.ProfessionDes, ProfessionImg: resDt.ProfessionImg, ProfessionCon: resDt.ProfessionCon, Recommend: resDt.Recommend, UserAccount: resDt.UserAccount, ClicksNumber: resDt.ClicksNumber})
	_, err = sess.Where("profession_id=?", resDt.ProfessionId).Cols("recommend").Update(&st.ProfessionMod{Recommend: resDt.Recommend})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + resDt.ProfessionTitle + " 编辑成功!"})
}

func professionDel(c *gin.Context) {
	ProfessionId := c.Query("ProfessionId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			c.JSON(409, "")
			return
		}
	}(mysql)
	_, err = mysql.Where("profession_id=?", ProfessionId).Delete(&st.ProfessionMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + ProfessionId + " 删除成功!"})
}

func professionType(c *gin.Context) {
	typeId := c.Query("tyeId")
	parentId := c.Query("parentId")
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)

	var collArr []*st.ProfessionTypeMod
	if typeId != "" {
		err = mysql.Where("type_id=?", typeId).OrderBy("create_time").Find(&collArr)
	} else if parentId != "" {
		err = mysql.Where("parent_id=?", parentId).OrderBy("create_time").Find(&collArr)
	} else {
		err = mysql.OrderBy("create_time").Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func professionTypeAdd(c *gin.Context) {
	resDt := &st.ProfessionTypeMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()

	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Cols("type_name", "parent_id", "type_img", "create_time", "update_time").InsertOne(&st.ProfessionTypeMod{TypeName: resDt.TypeName, ParentId: resDt.ParentId, TypeImg: resDt.TypeImg, CreateTime: t, UpdateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "添加" + resDt.TypeName + "成功!"})
}

func professionTypeEdit(c *gin.Context) {
	resDt := &st.ProfessionTypeMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	// mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	_, err = sess.Where("type_id=?", resDt.TypeId).Update(&st.ProfessionTypeMod{TypeName: resDt.TypeName, ParentId: resDt.ParentId, TypeImg: resDt.TypeImg})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + strconv.Itoa(resDt.TypeId) + " 编辑成功!"})
}

func professionTypeDel(c *gin.Context) {
	typeId := c.Query("typeId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	_, err = mysql.Where("type_id=?", typeId).Delete(&st.ProfessionTypeMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + typeId + " 删除成功!"})
}
